package com.sc.nft.task;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.*;
import com.sc.nft.dto.SendMessageMassReq;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribe;
import com.sc.nft.entity.DigitalCollectionProductInfoSubscribeWinBallot;
import com.sc.nft.entity.SecondOrder;
import com.sc.nft.entity.vo.DigitalCollectionProductInfoDrawVo;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.feign.SmsFeignClient;
import com.sc.nft.service.*;
import com.sc.nft.vo.WinNoticePeopleVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
@RequiredArgsConstructor
@Slf4j
public class SubscribeTask {

    private final DigitalCollectionProductInfoSubscribeDao digitalCollectionProductInfoSubscribeDao;
    private final DigitalCollectionProductInfoSubscribeCodeService digitalCollectionProductInfoSubscribeCodeService;
    private final DigitalCollectionProductInfoSubscribeWinBallotDao digitalCollectionProductInfoSubscribeWinBallotDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;


    @XxlJob("drawLots")
    public void drawLots(){
        DateTime offset = DateTime.now().offset(DateField.SECOND, 30);//未来30秒
        List<DigitalCollectionProductInfoSubscribe> digitalCollectionProductInfoSubscribes = digitalCollectionProductInfoSubscribeDao.drawLots(offset);
        // 获取开启时间在30秒内的抽签商品
        digitalCollectionProductInfoSubscribes.forEach(digitalCollectionProductInfoSubscribe -> {
            digitalCollectionProductInfoSubscribeCodeService.drawLots(digitalCollectionProductInfoSubscribe.getProductId());
        });
    }

    @XxlJob("resultAnnounce")
    public void resultAnnounce(){
        List<DigitalCollectionProductInfoDrawVo> digitalCollectionProductInfoDrawVos = digitalCollectionProductInfoSubscribeWinBallotDao.getResultAnnounce();
        digitalCollectionProductInfoDrawVos.forEach(digitalCollectionProductInfoDrawVo -> {
            //double check 判断申购商品是否存在
            Assert.isTrue(ObjectUtil.isNotNull(digitalCollectionProductInfoDao.getById(digitalCollectionProductInfoDrawVo.getProductId())),()->new GlobalRunTimeException("该申购商品不存在"));
            //组建短信模板时间参数
            List<WinNoticePeopleVo> winNoticePeopleVos = digitalCollectionProductInfoSubscribeWinBallotDao.getByUserTelAndStatus(digitalCollectionProductInfoDrawVo.getProductId());
            if (CollUtil.isNotEmpty(winNoticePeopleVos)) {
                winNoticePeopleVos.forEach(winNoticePeopleVo -> {
                    digitalCollectionProductInfoSubscribeCodeService.resultAnnounceSendMessage(winNoticePeopleVo,digitalCollectionProductInfoDrawVo.getProductId());
                });
            }
            digitalCollectionProductInfoSubscribeDao.updateNoticeStatusByProductId(digitalCollectionProductInfoDrawVo.getProductId());
        });
    }
}
